package com.lanp.gencode.test;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.*;

//解析xml
public class t11 {
    public static void main(String[] args) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("E:\\code\\jp\\code-test\\b.xml"));
        Element page = document.getRootElement();
        XmlNode pageNode = toXmlNode(page, page.attributes());
        pageNode.setXmlNodesList(new ArrayList<>());
        pageNode.setXpath("/" + pageNode.getNodeName());
        Iterator it = page.elementIterator();
        while (it.hasNext()) {
            System.out.println("=====开始遍历=====");
            Element ele = (Element) it.next();
            // 获取book的属性名以及 属性值
            XmlNode xmlNode = toXmlNode(ele, ele.attributes());
            xmlNode.setXpath(pageNode.getXpath() + "/" + xmlNode.getNodeName());
            pageNode.getXmlNodesList().add(xmlNode);
            List elements = ele.elements();
            forGetXmlNode(xmlNode, elements);
            System.out.println("=====结束遍历某=====");
        }
        System.out.println(JSON.toJSON(pageNode));
    }


    private static void forGetXmlNode(XmlNode xmlNode, List elements) {
        boolean isList = elements.size() > 1;
        xmlNode.setXmlNodesList(new ArrayList<>());
        for (int i = 0; i < elements.size(); i++) {
            Element eleChild = (Element) elements.get(i);
            XmlNode xmlNodeChild = toXmlNode(eleChild, eleChild.attributes());
            xmlNodeChild.setXpath(xmlNode.getXpath() + "/" + xmlNodeChild.getNodeName() + (isList ? ("[" + (i + 1) + "]") : ""));
            xmlNode.getXmlNodesList().add(xmlNodeChild);
            if(StringUtils.equals(xmlNodeChild.getText(),"立即领现金")){
                System.out.println("---------------------");
                System.out.println(xmlNodeChild.getXpath());
                System.out.println("---------------------");
            }
            List iterator = eleChild.elements();
            forGetXmlNode(xmlNodeChild, iterator);
        }
    }
//
///hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]
    private static XmlNode toXmlNode(Element ele, List<Attribute> bookAttrs) {
        Map<String, String> attrMap = new HashMap<>();
        for (Attribute attr : bookAttrs) {
            if(StringUtils.equals(attr.getValue(),"立即领现金")){
                System.out.println("3333");
            }
            attrMap.put(attr.getName(), attr.getValue());
        }
        XmlNode xmlNode = BeanUtil.mapToBean(attrMap, XmlNode.class, true);
        xmlNode.setNodeName(ele.getName());
        return xmlNode;
    }


    @Data
    public static class XmlNode {
        private String xpath;
        private String nodeName;
        private String index;
        private String package_;
        private String class_;
        private String text;
        private List<XmlNode> xmlNodesList;
    }
}
